//
//  personalVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/18.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "personalVC.h"
#import "headCell.h"
#import "setVC.h"
#import "moneyDetialVC.h"
#import "returnMoneyRecord.h"
#import "MyPromotion.h"
#import "myOrder.h"
#import "userCenterModel.h"
#import "mainHongBaoVC.h"
#import "logInVCViewController.h"
#import "PaymentFullVC.h"//test
#import "payVC.h"//test
@interface personalVC () <UITableViewDelegate, UITableViewDataSource> {
    userCenterModel *model;
    int times; // 标示非登录状态请求数据返回非登录的次数
}
@property (nonatomic, strong) UITableView *detialView;

@end

@implementation personalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - TabbarSafeBottomMargin);
    self.detialView = [[UITableView alloc]initWithFrame:frame];
    self.detialView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.detialView.tableFooterView = [UIView new];
    if (@available(iOS 11.0, *)) {
        self.detialView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.detialView.delegate = self;
    self.detialView.dataSource = self;
    [self.detialView registerNib:[UINib nibWithNibName:NSStringFromClass([headCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([headCell class])];
    [self.view addSubview:self.detialView];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self userRequest];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
    if (times > 1) {
        [self.navigationController popViewControllerAnimated:YES];
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}
#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;//多一个分区，用来隐藏最后一条下划线
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1) {
         return 3;
    }else if (section == 2) {
        return 2;
    }else if (section == 3) {
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 250;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) return 0;
    return 10;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
        // Remove seperator inset
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
    
        // Prevent the cell from inheriting the Table View's margin settings
        if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }
    
        // Explictly set your cell's layout margins
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        headCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([headCell class])];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont customFontOfSize:13];
    }
   
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"order"];
            cell.textLabel.text = @"我的订单";
        }else if (indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"horn"];
            cell.textLabel.text = @"我的推广";
        }else if (indexPath.row == 2) {
            cell.imageView.image = [UIImage imageNamed:@"hongBaoHead"];
            cell.textLabel.text = @"我的红包";
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"record"];
            cell.textLabel.text = @"还款记录";
        }else if (indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"detail"];
            cell.textLabel.text = @"资金明细";
        }
    }else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"setUp"];
            cell.textLabel.text = @"设置";
            cell.separatorInset = UIEdgeInsetsMake(0,0, 0, cell.bounds.size.width-15);
        }else if (indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"customerService"];
            cell.textLabel.text = @"客服";
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            setVC *VC =[setVC new];
            [self.navigationController pushViewController:VC animated:YES];
        }else {
       
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 1) {
            moneyDetialVC *VC = [moneyDetialVC new];
            [self.navigationController pushViewController:VC animated:YES];
        }else {
            returnMoneyRecord *VC = [returnMoneyRecord new];
            [self.navigationController pushViewController:VC animated:YES];
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            MyPromotion *VC = [MyPromotion new];
            [self.navigationController pushViewController:VC animated:YES];
        }else if(indexPath.row == 0) {
            myOrder *VC = [myOrder new];
            [self.navigationController pushViewController:VC animated:YES];
        }else {
            mainHongBaoVC *VC = [mainHongBaoVC new];
            [self.navigationController pushViewController:VC animated:YES];
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    grayView.backgroundColor = [UIColor clearColor];
    return grayView;
}

#pragma mark ---
- (void)userRequest {
    if (USERTOKEN == nil) {
        times++;
        if (times == 1) {
            [self logInVC];
        }
        return;
    }
    NSDictionary *dic = @{
                          @"token":USERTOKEN
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] userCenter]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            model = [userCenterModel mj_objectWithKeyValues:responseObject[@"data"]];
            [weakSelf.detialView reloadData];
        }else{
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)logInVC {
    logInVCViewController *VC = [logInVCViewController new];
    VC.btnHidden = @"NO";
    [self presentViewController:VC animated:YES completion:nil];
    
}
- (void)dealloc {
    
}

@end
